package com.icard.wx.icardwx.service.impl;

import com.icard.wx.icardwx.model.AccessToken;
import com.icard.wx.icardwx.service.IWxService;
import com.icard.wx.icardwx.utils.PropertyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class WxService implements IWxService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public AccessToken getAccessToken(String code) {
        StringBuilder sb = new StringBuilder();
        sb.append(PropertyUtil.ACCESSTOEKN_AUTH_URL).append(String.format("?appid=%s&secret=%s&code=%s&grant_type=authorization_code", PropertyUtil.getAppid(), PropertyUtil.getAppsecret(),code));
        AccessToken accessToken =  restTemplate.getForObject(sb.toString(), AccessToken.class);
        return accessToken;
    }
}
